package com;

import java.util.LinkedList;
import java.util.Random;

public class Snake {
	public static final int RIGHT = 1;
	public static final int LEFT = 3;
	public static final int UP = 2;
	public static final int DOWN = 4;
	public static final int RUNNING = 10;
	public static final int PAUSE = 11;
	public static final int OVER = 12;
	public static final int START = 13;
	public LinkedList<Node> body = new LinkedList<Node>();
	private int direction = RIGHT;
	private int state = START;
	private Node food;
	public int score;
	public int speed;

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public Snake() {

		body.add(new Node(60, 20));
		body.add(new Node(40, 20));
		body.add(new Node(20, 20));
		makefood();
	}

	public void move() {
		Node tail = body.pollLast();
		Node head = body.getFirst();
		switch (direction) {
		case RIGHT:
			tail.setX(head.getX() + Node.WIDTH);
			tail.setY(head.getY());
			break;
		case LEFT:
			tail.setX(head.getX() - Node.WIDTH);
			tail.setY(head.getY());
			break;
		case UP:
			tail.setY(head.getY() - Node.HEIGHT);
			tail.setX(head.getX());
			break;
		case DOWN:
			tail.setY(head.getY() + Node.HEIGHT);
			tail.setX(head.getX());
			break;
		}

		body.addFirst(tail);
	}

	public void eat() {
		Node head = body.getFirst();
		if (head.getX() == 500 || head.getX() + Node.WIDTH == 0 || head.getY() == 500
				|| head.getY() + Node.HEIGHT == 0) {
			this.setState(OVER);
		}
		for (int i = 1; i < this.body.size(); i++) {
			if (head.getX() == body.get(i).getX() && head.getY() == body.get(i).getY()) {
				this.setState(OVER);
			}
		}
		boolean isEat = false;
		if (this.direction == RIGHT && head.getY() == food.getY() && (head.getX() + Node.WIDTH) == food.getX()) {
			isEat = true;
		} else if (this.direction == LEFT && head.getY() == food.getY() && (head.getX() - Node.WIDTH) == food.getX()) {
			isEat = true;
		} else if (this.direction == DOWN && head.getX() == food.getX() && (head.getY() + Node.HEIGHT) == food.getY()) {
			isEat = true;
		} else if (this.direction == UP && head.getX() == food.getX() && (head.getY() - Node.HEIGHT) == food.getY()) {
			isEat = true;
		}

		if (isEat) {
			score = score + 10;
			body.addFirst(food);
			makefood();
		}

	}

	public void makefood() {

		boolean isInBody = false;
		int x, y;
		do {
			Random r = new Random();
			y = r.nextInt(25) * Node.HEIGHT;
			x = r.nextInt(25) * Node.WIDTH;
			for (Node node : body) {
				if (x == node.getX() && y == node.getY()) {
					isInBody = true;
					break;
				}
			}
		} while (isInBody == true);
		food = new Node();
		food.setX(x);
		food.setY(y);
	}

	public Node getFood() {
		return food;
	}

	public void changeDirection(int direction) {
		if (this.direction % 2 != direction % 2)
			this.direction = direction;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

}
